<?php
include __DIR__ . "/config/cors.php";
// 本地路径
$local_dir = __DIR__ . "/upload/";
// 远程下载路径
$remote_url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . "/upload/";
// 本地路径遍历
$files = scandir($local_dir);
if (sizeof($files) == 2 and in_array(".", $files) and in_array("..", $files)) {
    echo json_encode([
        "code" => 201,
        "message" => "当前目录下文件为空",
    ]);
    exit;
}
$file_list = [];
foreach ($files as $file) {
    // 排除 . ..
    if ($file != "." and $file != "..") {
        // 文件大小
        $file_size = round(filesize($local_dir . $file) / 1024, 2);
        // 文件类型
        $file_type = pathinfo($file, PATHINFO_EXTENSION);
        $file_list[] = [
            "file_url" => "$remote_url$file",
            "file_type" => $file_type,
            "file_size" => "$file_size KB",
        ];
    }
}
echo json_encode([
    "code" => 200,
    "message" => "文件读取成功",
    "data" => $file_list,
]);

